package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.modular.system.model.ChangeLog;
import cn.stylefeng.guns.modular.system.dao.ChangeLogMapper;
import cn.stylefeng.guns.modular.system.model.GameUser;
import cn.stylefeng.guns.modular.system.service.IChangeLogService;
import cn.stylefeng.guns.modular.system.service.IGameUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.nineshadow.games.Enum.GameTypeEnum;
import com.nineshadow.games.Enum.TypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 金币变化记录 服务实现类
 * </p>
 *
 * @author liyong
 * @since 2019-11-28
 */
@Service
public class ChangeLogServiceImpl extends ServiceImpl<ChangeLogMapper, ChangeLog> implements IChangeLogService {

    @Autowired
    private IGameUserService gameUserService;

    @Override
    public void saveChangeLog(Integer userId, Integer source_id, String scurce_table,
                              Integer type, Integer change_type, BigDecimal change_num,Integer gameType) throws Exception {
            GameUser gameUser=gameUserService.selectById(userId);
            ChangeLog changeLog=new ChangeLog();
            changeLog.setUserId(userId);
            changeLog.setSourceId(source_id);
            changeLog.setScurceTable(scurce_table);
            changeLog.setChangeNum(change_num);
            changeLog.setType(type);
            changeLog.setCreateTime(new Date());
            changeLog.setChangeType(change_type);
            changeLog.setGameType(gameType);
            switch (TypeEnum.getByCode(type)){
                case SCORE:{
                    if(gameType== GameTypeEnum.XXL.getType()){
                        BigDecimal afterNum=gameUser.getScore().add(change_num);
                        changeLog.setBeforeNum(gameUser.getScore());
                        changeLog.setAfferNum(afterNum.compareTo(BigDecimal.ZERO)<0?BigDecimal.ZERO:afterNum);
                        gameUser.setScore(changeLog.getAfferNum());
                    }else{
                        BigDecimal afterNum=gameUser.getMcScore().add(change_num);
                        changeLog.setBeforeNum(gameUser.getMcScore());
                        changeLog.setAfferNum(afterNum.compareTo(BigDecimal.ZERO)<0?BigDecimal.ZERO:afterNum);
                        gameUser.setMcScore(changeLog.getAfferNum());
                    }
                    gameUserService.updateById(gameUser);
                    break;
                }
                case COINS:{
                    if(gameType== GameTypeEnum.XXL.getType()) {
                        changeLog.setBeforeNum(BigDecimal.valueOf(gameUser.getCoins()));
                        changeLog.setAfferNum(BigDecimal.valueOf(gameUser.getCoins()).add(change_num));
                        gameUser.setCoins(changeLog.getAfferNum().intValue());
                    }else{
                        changeLog.setBeforeNum(BigDecimal.valueOf(gameUser.getMcCoins()));
                        changeLog.setAfferNum(BigDecimal.valueOf(gameUser.getMcCoins()).add(change_num));
                        gameUser.setMcCoins(changeLog.getAfferNum().intValue());
                    }
                    gameUserService.updateById(gameUser);
                    break;
                }
                default:break;
            }
            insert(changeLog);
        }
}
